<template>
  <div class="login-container">
    <h1 class="title">
      宏宇集团汽车订单系统
    </h1>
    <el-row :gutter="40">
      <el-col :offset="9" :span="6">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="off"
          label-position="left"
        >
          <el-form-item prop="userCode">
            <el-input
              v-model="loginForm.userCode"
              name="userCode"
              type="text"
              auto-complete="off"
              placeholder="请输入账号"
              clearable
            >
              <template slot="prepend">
                账号
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="userPassword">
            <el-input
              v-model="loginForm.userPassword"
              :type="pwdType"
              name="userPassword"
              auto-complete="off"
              placeholder="请输入密码"
              clearable
              @keyup.enter.native="handleLogin"
            >
              <template slot="prepend">
                密码
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              登 录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.trim().length === 0) {
        callback(new Error('请输入账号'))
      } else if (value.match(/[\u4e00-\u9fa5]/ig)) {
        callback(new Error('请输入正确的账号'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.trim().length === 0) {
        callback(new Error('请输入密码'))
      } else if (value.trim().length < 6) {
        callback(new Error('密码不能小于6位'))
      } else if (value.match(/\`|\@/ig)) {
        callback(new Error('密码不能含有特殊字符'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userCode: '',
        userPassword: ''
      },
      loginRules: {
        userCode: [{ required: true, trigger: 'blur', validator: validateUsername }],
        userPassword: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            // this.$router.push({ path: this.redirect || '/' })
            this.$router.push({ path: '/' }, () => {})
          }).catch(() => {
            this.loading = false
            this.$store.dispatch('FedLogOut')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
// .login-container {
//   .el-input {
//     display: inline-block;
//     height: 47px;
//     width: 85%;
//     input {
//       background: transparent;
//       border: 0px;
//       -webkit-appearance: none;
//       border-radius: 0px;
//       padding: 12px 5px 12px 15px;
//       color: $light_gray;
//       height: 47px;
//       &:-webkit-autofill {
//         -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
//         -webkit-text-fill-color: #fff !important;
//       }
//     }
//   }
//   .el-form-item {
//     border: 1px solid rgba(255, 255, 255, 0.1);
//     background: rgba(0, 0, 0, 0.1);
//     border-radius: 5px;
//     color: #454545;
//   }
// }
.login-container {
  .el-form-item__error {
    padding-left: 87px;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("./../../static/login-bg3.jpg") no-repeat center center;
  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 30px auto 130px;
    background: rgba(3, 98, 162, 0.2);
    border-radius: 8px;
    transform: scale(1.3);
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .left-img {
    width: 100%;
    height: auto;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    width: 1200px;
    height: 110px;
    line-height: 110px;
    font-size: 60px;
    font-weight: 400;
    color: $light_gray;
    margin: 130px auto 80px auto;
    text-align: center;
    font-weight: bold;
    background: url("./../../static/top.png") no-repeat center center;
    background-size: 1200px 100%;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
